<?php
/*********************/
/*                   */
/* http://www.ymd.cc */
/*********************/
/*    QQ: 11121426   */
/*                   */

/*********************/

class http_base
{
    public $timeout = 10;
    public $defaultChunk = 4096;
    public $http_ver = "1.1";
    public $responseHeader = array();
    public $responseBody = "";
    public $proxyHost = null;

    public function action($action, $url, $headers = null, $callback = null, $data = null)
    {
        $url_info = parse_url($url);
        $out = strtoupper($action) . " " . (isset($url_info['path']) ? $url_info['path'] : "/") . (isset($url_info['query']) ? "?" . $url_info['query'] : "") . " HTTP/{$this->http_ver}\r\n";
        $host = isset($url_info['port']) ? $url_info['host'] . ":" . $url_info['port'] : $url_info['host'];
        $out .= "Host: " . $host . "\r\n";
        if ($data) {
            if (is_array($data)) {
                $data = http_build_query($data);
            }
            $headers['Content-length'] = strlen($data);
            if (!isset($headers['Content-Type'])) {
                $headers['Content-Type'] = "application/x-www-form-urlencoded";
            }
        }
        $headers["Accept"] = "*/*";
        $headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)";
        $headers['Pragma'] = "no-cache";
        $headers['Cache-Control'] = "no-cache";
        $headers['Connection'] = "close";
        $headers['Referer'] = "http://www.shopex.cn/";
        foreach ($headers as $k => $v) {
            $k = trim($k);
            $v = trim($v);
            if ($k != "" && $v != "") {
                $out .= $k . ":" . $v . "\r\n";
            }
        }
        if ($data)
            $out .= "\r\n" . $data;
        else $out .= "\r\n";
        $data = null;
        $fp = fsockopen($this->proxyHost ? $this->proxyHost : $url_info['host'], $this->proxyPort ? $this->proxyPort : isset($url_info['port']) ? $url_info['port'] : 80, $errno, $errstr, $this->timeout);
        if ($fp) {
            //����http����ͷ��
            fwrite($fp, $out);
            $out = null;
            //��ȡhttp������Ӧ
            $status = fgets($fp, 512);
            if (preg_match("/\\d{3}/", $status, $match)) {
                $this->responseCode = $match[0];
            }
            while (!feof($fp)) {
                $line = trim(fgets($fp, 512));
                if ($line == "") break;
                /*if ( $p = strpos( $raw, ":" ) )
                {
                    $tm=strtolower( trim( substr( $raw, 0, $p ) ) );
                    $responseHeader[strtolower( trim( substr( $raw, 0, $p ) ) )] = trim( substr( $raw, $p + 1 ) );
                    continue;
                }*/
                $hkey = "";
                $hvalue = "";
                $v = 0;
                for ($i = 0; $i < strlen($line); $i++) {
                    if ($v == 1) $hvalue .= $line[$i];
                    if ($line[$i] == ":") $v = 1;
                    if ($v == 0) $hkey .= $line[$i];
                }
                $hkey = trim($hkey);
                if ($hkey != "") $this->responseHeader[strtolower($hkey)] = trim($hvalue);

            }
            if (isset($this->responseHeader['location'])) {
                return $this->action($action, $this->responseHeader['location'], $headers, $callback);
            }
            if (!($chunkmode = isset($this->responseHeader['transfer-encoding']) && $this->responseHeader['transfer-encoding'] == "chunked")) {
                $chunklen = $this->defaultChunk;
            }
            //��ȡ$fp�ļ���
            while (!feof($fp) && (!$chunkmode || ($chunklen = hexdec(trim($a = fgets($fp, 30)))))) {
                $content = fread($fp, $chunklen);
                $readlen = strlen($content);
                while ($chunklen != $readlen) {
                    $buffer = fread($fp, $chunklen - $readlen);
                    if (!strlen($buffer)) {
                        break;
                    }
                    $readlen += strlen($buffer);
                    $content .= $buffer;
                }
                //�ص���������action������GET���򱣴������ص�����
                //������ctl_download��: �ص�������_runner_handle
                if ($callback) {
                    call_user_func_array($callback, array(
                        $this,
                        $content
                    ));
                }

                if (!$callback) {
                    $this->responseBody .= $content;
                }
                if ($chunkmode) {
                    fread($fp, 2);
                }
            }
            fclose($fp);
            if ($callback) {
                return $this->responseCode[0] == 2;
            } else {
                if ($this->responseCode[0] == 2) {
                    return $this->responseBody;
                } else {
                    $this->errorInfo = $this->responseBody;
                    return false;
                }
            }
        } else {

            return false;
        }
    }

}

?>
